//
//  YunOrderListViewModel.m
//  BaseProject
//
//  Created by 王盼盼 on 2024/10/21.
//  Copyright © 2024 wangpanpan. All rights reserved.
//

#import "YunOrderListViewModel.h"
#import "YunOrderListModel.h"
#import "YunOrderClassesModel.h"
@interface YunOrderListViewModel ()
@property(nonatomic, assign) NSInteger pageNum;
@property(nonatomic, assign) NSInteger pageSize;
@property(nonatomic, strong) NSMutableArray<YunOrderListModel *> *dataSource;
@property(nonatomic, strong) NSArray<YunOrderClassesModel *> *classes;
@property(nonatomic, copy) NSString *classId;
@end

@implementation YunOrderListViewModel
- (instancetype)init {
    self = [super init];
    if (self) {
        self.classId = @"";
    }
    return self;
}
- (void)transfrom {
    self.pageNum = 1;
    self.pageSize = 20;
    [self request];
    [self requestClases];
}
- (NSArray<YunOrderListModel *> *)dataArray {
    return self.dataSource.copy;
}
- (void)refresh:(BOOL)header {
    if (header) {
        self.pageNum = 1;
        [self.dataSource removeAllObjects];
    } else {
        self.pageNum += 1;
    }
    [self request];
}
- (void)resetClassId:(NSString *)classId {
    self.classId = classId;
    [self refresh:YES];
}
- (void)request {
    WK(weakSelf)
    NSMutableDictionary *tp = [NSMutableDictionary dictionary];
    [tp setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
    if (self.classId != nil && self.classId.length > 0) {
        [tp setValue:self.classId forKey:@"classId"];
    }
    
    NSMutableDictionary *requestMsg = [NSMutableDictionary dictionary];
    [requestMsg setValue:@(self.pageNum) forKey:@"pageNum"];
    [requestMsg setValue:@(self.pageSize) forKey:@"pageSize"];
    [requestMsg setValue:tp forKey:@"t"];
    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_GetYunOrderList];
    [SVProgressHUD showWithStatus:@"正在加载"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:requestMsg FinishBlock:^(id  _Nonnull data) {
        BaseResponse *response = [BaseResponse modelWithJSON:data];
        if (!response.success) {
            !weakSelf.refresh ?: weakSelf.refresh(YES);
            return;
        }
        NSArray *list = data[@"data"][@"list"];
        if (![list isKindOfClass:[NSArray class]]) {
            !weakSelf.refresh ?: weakSelf.refresh(YES);
            return;
        }
        [weakSelf.dataSource addObjectsFromArray:[YunOrderListModel mj_objectArrayWithKeyValuesArray:list]];
        !weakSelf.refresh ?: weakSelf.refresh(self.dataSource.count != [data[@"data"][@"total"]integerValue]);
        [SVProgressHUD dismiss];
    } FailedBlock:^(NSError * _Nonnull error) {
        !weakSelf.refresh ?: weakSelf.refresh(YES);
        [SVProgressHUD dismiss];
    }];
}
- (void)requestClases {
    if (self.classes.count > 0) {
        return;
    }
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:@"-1" forKey:@"parentId"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_ChildCategory];
    [SVProgressHUD showWithStatus:@"正在加载"];
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:parameters FinishBlock:^(id  _Nonnull data) {
        [SVProgressHUD dismiss];
        BaseResponse *response = [BaseResponse modelWithJSON:data];
        if (!response.success) {
            return;
        }
        NSArray *oneList = [data valueForKey:@"data"];
        if ([oneList isKindOfClass:[NSArray class]]) {
            NSMutableArray *tempArray = [NSMutableArray arrayWithArray:[YunOrderClassesModel mj_objectArrayWithKeyValuesArray:oneList]];
            YunOrderClassesModel *allModel = [[YunOrderClassesModel alloc]init];
            allModel.scId = @"";
            allModel.parentName = @"全部";
            [tempArray insertObject:allModel atIndex:0];
            weakSelf.classes = tempArray.copy;
        }
    } FailedBlock:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
    }];
}
#pragma mark - lazy
- (NSMutableArray<YunOrderListModel *> *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
@end
